package cn.thank.controller;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.org.apache.bcel.internal.generic.NEW;

import cn.thank.model.UseAccount;
import cn.thank.service.IUserService;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {

	
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value = "/index")
	public String init(Model model){
		int pageSize = 5;
		List<UseAccount> alluses = userService.getAll();
		model.addAttribute("totalcount",alluses.size());
		model.addAttribute("totalpage",alluses.size()/pageSize);
		System.out.println(alluses.size()/pageSize);
		// 分页查询
		List<UseAccount> uses = userService.queryByPage(0);
		model.addAttribute("users", uses);
		return "admin/index";
	}
	
	// 分页
	@RequestMapping(value = "/query")
	public String query(Model model,int pageNo){
		int pageSize = 5;
		List<UseAccount> alluses = userService.getAll();
		model.addAttribute("totalcount",alluses.size());
		model.addAttribute("totalpage",alluses.size()/pageSize);
		// 分页查询
		int startPage = (pageNo-1)*pageSize;
		List<UseAccount> uses = userService.queryByPage(startPage);
		model.addAttribute("users", uses);
		return "admin/index";
	}
	
	@RequestMapping(value = "/caculate")
	public String caculate(Model model,Integer days) throws Exception{
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        if(days == null) days = 30;
        String szDate = this.addDay(fmt.format(new Date()).toString(), days-1);
        Date limitedtime = fmt.parse(szDate);
        List<UseAccount> lAccounts = userService.getByLimitedtime(limitedtime);
		System.out.println(limitedtime+"=============="+lAccounts.size());
		int addtimes = 0;
		int addResponsetime = 0;
		int updateResponsetime = 0;
		for(int i=0;i<lAccounts.size();i++){
			if (lAccounts.get(i).getFunctionName().equals("新增")) {
				addtimes++;
				addResponsetime += lAccounts.get(i).getResponsetime();
			}if (lAccounts.get(i).getFunctionName().equals("修改")) {
				updateResponsetime += lAccounts.get(i).getResponsetime();
			}
		}
		int updatetimes = lAccounts.size() - addtimes;
		if(addtimes == 0) addResponsetime = 0;
		else addResponsetime = addResponsetime/addtimes;
		if(updatetimes == 0) updateResponsetime = 0;
		else updateResponsetime = updateResponsetime/updatetimes;
		model.addAttribute("addtimes",addtimes);
		model.addAttribute("updatetimes",updatetimes);
		model.addAttribute("addResponsetime",addResponsetime);
		model.addAttribute("updateResponsetime",updateResponsetime);
		model.addAttribute("days", days);
		return "admin/caculate";
	}
	
	// 实现日期加一天的方法
	public String addDay(String s, int n) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cd = Calendar.getInstance();
			cd.setTime(sdf.parse(s));
			cd.add(Calendar.DATE, -n);// 增加一天
			// cd.add(Calendar.MONTH, n);//增加一个月
			return sdf.format(cd.getTime());
		} catch (Exception e) {
			return null;
		}
	}
	
}
